//
//  ViewController.m
//  mintHome
//
//  Created by 朱彦名 on 16/6/15.
//  Copyright © 2016年 朱彦名. All rights reserved.
//

#import "ViewController.h"
#import "ViewOfTitle.h"
#import "scrollView.h"
#import "tableViewOfFirst.h"
#import "cellOfFirst.h"
#import "album.h"
#import "modelOfMintImage.h"
#import "NetWorkingHandler.h"
#import "modelOfMintHome.h"
#import <SDWebImage/UIImageView+WebCache.h>
#import "viewOfTag.h"
#import "tapGestureViewController.h"
#import "secondTableViewController.h"
#import "thirdTableViewController.h"
#import "foutThTableViewController.h"
#import "fiveTableViewController.h"
#import "searchViewController.h"
#import "DataOfModel.h"

@interface ViewController ()<UITableViewDataSource, UITableViewDelegate, UIWebViewDelegate>

@property (nonatomic, retain) ViewOfTitle *viewOfTitle;

@property (nonatomic, retain) scrollView *scrollOfMintHome;

@property (nonatomic, retain) tableViewOfFirst *tableview1;

@property (nonatomic, retain) album *albumOfImage;

@property (nonatomic, retain) NSMutableArray *arrOfImages;

@property (nonatomic, retain) NSMutableArray *arrOfImages1;

@property (nonatomic, retain) NSMutableArray *arrOfMint;

@property (nonatomic, retain) NSMutableArray *arrOfData;

@property (nonatomic, retain) NSMutableArray *arr1;

@property (nonatomic, retain) UIView *viewOfScrol;

@property (nonatomic, retain) NSMutableArray *arrOfReBtn;

@property (nonatomic, retain)UIButton *button1;




@end

@implementation ViewController
-(void)dealloc{

    [_viewOfTitle release];
    [_scrollOfMintHome release];
    [_tableview1 release];
    [_albumOfImage release];
    [_arrOfImages1 release];
    [_arrOfImages release];
    [_arrOfMint release];
    [_arrOfData release];
    [_arr1 release];
    [_viewOfScrol release];
    [_arrOfReBtn release];
    [_button1 release];
    [super dealloc];
    
}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
    [self setNavigition];
    [self creatViewOfTitle];
    [self creatScrollViewOfFirst];
    [self creatTableViewOfFirst];
    self.arrOfMint = [NSMutableArray array];
    [self handleOfTableView];
    
    self.tableview1.allowsSelection = YES;
    self.tableview1.tableHeaderView = [self creatAlbum];
    
    [self creatKVOAction];
    [self refresh];
    

    
    
}
#pragma mark - 界面布局
- (void)setNavigition{
    self.navigationItem.title = @"易家居";
    
    self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc]initWithBarButtonSystemItem:UIBarButtonSystemItemSearch target:self action:@selector(searchMint:)];
}
#pragma mark - 搜索
- (void)searchMint: (UIBarButtonItem *)button{

//    NSLog(@"搜索失败");
    searchViewController *searchView = [[searchViewController alloc]init];
    
    [self.navigationController pushViewController:searchView animated:YES];
    
    
    
}
- (void)creatViewOfTitle{

    self.viewOfTitle = [[ViewOfTitle alloc]initWithFrame:CGRectMake(0, 64, [UIScreen mainScreen].bounds.size.width, 40)];
    //self.viewOfTitle.backgroundColor = [UIColor yellowColor];
    [self.view addSubview:_viewOfTitle];
    [_viewOfTitle release];
    
    self.viewOfScrol = [[UIView alloc]initWithFrame:CGRectMake(0, 32, self.viewOfTitle.frame.size.width / 5, 1)];
    self.viewOfScrol.backgroundColor = [UIColor redColor];
    [self.viewOfTitle addSubview:self.viewOfScrol];
    
    NSArray *buttons = @[@"精选", @"卧室", @"卫浴", @"厨房", @"客厅"];
    self.arrOfReBtn = [NSMutableArray arrayWithCapacity:5];
    for (int i = 0; i < 5; i++) {
        UIButton *buttonOfTitle = [UIButton buttonWithType:UIButtonTypeSystem];
        buttonOfTitle.frame = CGRectMake(SCREEN_WIDTH / 5 * i, 0, SCREEN_WIDTH / 5, 30);
        [self.viewOfTitle addSubview:buttonOfTitle];
        buttonOfTitle.tag = i;
        [buttonOfTitle setTitle:buttons[i] forState:UIControlStateNormal];
        [self.arrOfReBtn addObject:buttonOfTitle];
        [buttonOfTitle addTarget:self action:@selector(buttonPass:) forControlEvents:UIControlEventTouchDown];
        
        [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(setLightDay) name:@"day" object:nil];
        [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(setNightDay) name:@"night" object:nil];
    }
    
    
    BOOL model = [AccountManager sharedInstance].isNight;
    if (model == YES) {
        
        _button1.backgroundColor = [UIColor silverColor];
        
    }
    else{
        
        _button1.backgroundColor = [UIColor whiteColor];
        
    }
    
}
//点击title上的button 可以实现页面的跳转
- (void)buttonPass: (UIButton *)sender{

    for (UIButton *button in self.arrOfReBtn) {
        if (button.tag == sender.tag) {
            self.scrollOfMintHome.contentOffset = CGPointMake(SCREEN_WIDTH * sender.tag, 0);
            
        }

    }
}
- (void)creatScrollViewOfFirst{

    self.scrollOfMintHome = [[scrollView alloc]init];
    [self.view addSubview:self.scrollOfMintHome];
    self.scrollOfMintHome.frame = CGRectMake(0, 104, [UIScreen mainScreen].bounds.size.width, SCREEN_HEIGHT);
    self.scrollOfMintHome.backgroundColor = [UIColor whiteColor];
    self.scrollOfMintHome.contentSize = CGSizeMake(SCREEN_WIDTH * 5, SCREEN_HEIGHT);
    self.scrollOfMintHome.pagingEnabled = YES;
    
    
}
#pragma mark - 滚动条滑动  button点击跳转
- (void)creatKVOAction{

    [self.scrollOfMintHome addObserver:self forKeyPath:@"contentOffset" options:NSKeyValueObservingOptionNew | NSKeyValueObservingOptionOld  context:@"d"];

}
- (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary<NSString *,id> *)change context:(void *)context{
    CGFloat x = [[change objectForKey:@"new" ]CGPointValue].x;
    
    CGFloat redx = x * SCREEN_WIDTH / 5 / SCREEN_WIDTH ;
    
    self.viewOfScrol.transform = CGAffineTransformMakeTranslation(redx, 0);
    
}

- (void)creatTableViewOfFirst{

    self.tableview1 = [[tableViewOfFirst alloc]initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT) style:UITableViewStylePlain];
    self.tableview1.delegate = self;
    self.tableview1.dataSource = self;
    [self.scrollOfMintHome addSubview:self.tableview1];
    [self.tableview1 registerClass:[cellOfFirst class] forCellReuseIdentifier:@"pool"];
    
   

    secondTableViewController *secondController = [[secondTableViewController alloc]init];
    [self addChildViewController:secondController];
    
    secondController.tableView.frame = CGRectMake(SCREEN_WIDTH, 0, SCREEN_WIDTH,SCREEN_HEIGHT);
    [self.scrollOfMintHome addSubview:secondController.tableView];
        
    
        
    
    
    thirdTableViewController *thirdController = [[thirdTableViewController alloc]init];
    [self addChildViewController:thirdController];
    
    thirdController.tableView.frame = CGRectMake(SCREEN_WIDTH * 2, 0, SCREEN_WIDTH, SCREEN_HEIGHT);
    [self.scrollOfMintHome addSubview:thirdController.tableView];
    
    foutThTableViewController *fourController = [[foutThTableViewController alloc]init];
    [self addChildViewController:fourController];
    fourController.tableView.frame = CGRectMake(SCREEN_WIDTH * 3, 0, SCREEN_WIDTH, SCREEN_HEIGHT);
    [self.scrollOfMintHome addSubview:fourController.tableView];
    

    fiveTableViewController *fiveController = [[fiveTableViewController alloc]init];
    [self addChildViewController:fiveController];
    fiveController.tableView.frame = CGRectMake(SCREEN_WIDTH * 4, 0, SCREEN_WIDTH, SCREEN_HEIGHT);
    [self.scrollOfMintHome addSubview:fiveController.tableView];
    
    

}

- (UIView *)creatAlbum{
    self.albumOfImage = [[album alloc]initWithFrame:CGRectMake(0, 0, [UIScreen mainScreen].bounds.size.width, SCREEN_BOUNDS.size.height / 3) withImage:[self handleImage] ];
    self.albumOfImage. userInteractionEnabled = YES;
    
    return self.albumOfImage;

}



#pragma mark - 协议方法


- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return _arrOfMint.count;
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{

    return 200;
}


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    cellOfFirst *cell = [tableView dequeueReusableCellWithIdentifier:@"pool" forIndexPath:indexPath];
    
    
    modelOfMintHome *model1 = _arrOfMint[indexPath.row];
    
    
    
    BOOL model = [AccountManager sharedInstance].isNight;
    if (model == YES) {
        
        cell.backgroundColor = [UIColor silverColor];
        
    }
    else{
        
        cell.backgroundColor = [UIColor whiteColor];
        
    }
    
    /** cell赋值  */
    [cell.imageOfMint sd_setImageWithURL:[NSURL URLWithString:model1.cover_image_url]];
    
    
    cell.labelOfTitle.text = model1.title;
    
    
    NSString *str = [NSString stringWithFormat:@"%ld",model1.likes_count];
    
    cell.viewOfTag.labelOfTag.text = str;
    
    
    return cell;


}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    
    tapGestureViewController * tapView = [[tapGestureViewController alloc]init];
    
    self.arr1 = [NSMutableArray array];
    
    
    for (NSDictionary *dic in self.arrOfData) {
        
        [self.arr1 addObject: [dic objectForKey:@"url"]];
        
    }
    tapView.url1 = self.arr1[indexPath.row];
    
    [self.navigationController pushViewController:tapView animated:YES];
    
    
}
#pragma mark - 解析数据
- (NSArray *)handleImage{
    self.arrOfImages = [NSMutableArray array];
    self.arrOfImages1 = [NSMutableArray array];
    
    

    NSURL *url = [NSURL URLWithString:@"http://api.bohejiaju.com/v1/banners?channel=iOS"];
    NSData *data = [NSData dataWithContentsOfURL:url];
    
    id result = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableLeaves error:nil];
    
    NSArray *arrOfData = result[@"data"][@"banners"];
    
    
   
    
    for (NSDictionary *dic in arrOfData) {
        modelOfMintImage *model = [[modelOfMintImage alloc]initWithDic:dic];
        
        //[self.arrOfImages addObject:model];
        
        NSURL *url = [NSURL URLWithString:model.image_url];
        NSData *data = [NSData dataWithContentsOfURL:url];
        UIImage *image = [UIImage imageWithData:data];
        [self.arrOfImages1 addObject:image];}
    
    return self.arrOfImages1;
    
}
- (void)handleOfTableView{
    
    
    [MBProgressHUDManager showHUDtoView:self.view string:@"正在加载"];
    
    
    [NetWorkingHandler handleDataWithUrl:@"http://api.bohejiaju.com/v1/channels/4/items?gender=1&generation=1&limit=20&offset=0" completion:^(id result, NSURLResponse *response, NSError *error) {
        self.arrOfData = result[@"data"][@"items"];
        for (NSDictionary *dic in self.arrOfData) {
            
            modelOfMintHome *model2 = [[modelOfMintHome alloc]init];
            
            [model2 initWithDic:dic];
            
            [self.arrOfMint addObject:model2];
            
        }
        [_tableview1 reloadData];
        [MBProgressHUDManager removeHUD];
        
    }];
    
    [_tableview1.mj_header endRefreshing];
    
}




- (void)setLightDay{
    
    
    [NightManager setBackgroundColorWithView:self.view];
    self.navigationController.navigationBar.barStyle = UIBarStyleDefault;
    [_tableview1 reloadData];
    
}
- (void)setNightDay{
    
    
    
    [NightManager setBackgroundColorWithView:self.view];
    self.navigationController.navigationBar.barStyle = UIBarStyleBlack;
    [_tableview1 reloadData];
    
}
- (void)refresh{
    
    self.tableview1.mj_header = [MJRefreshNormalHeader headerWithRefreshingTarget:self refreshingAction:@selector(handleOfTableView)];
    [self.tableview1.mj_header beginRefreshing];
                                 

}



@end
